import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { DictApi, Transfer } from '#/api';

import { reactive, ref } from 'vue';

import { DictResp, getTransferCode } from '#/api';

const status = reactive<DictApi.SelectOption[]>([]);
const type = reactive<DictApi.SelectOption[]>([]);
const yes = reactive<DictApi.SelectOption[]>([]);
DictResp(['mes_transfer_status', 'mes_transfer_type', 'sys_yes_no']).then(
  (res) => {
    if (res.mes_transfer_status) {
      status.push(...res.mes_transfer_status);
    }
    if (res.mes_transfer_type) {
      type.push(...res.mes_transfer_type);
    }
    if (res.sys_yes_no) {
      yes.push(...res.sys_yes_no);
    }
  },
);

export const mes_transfer_status = status;
export const mes_transfer_type = type;
export const sys_yes_no = yes;

const switchValue = ref('');
/**
 * 获取编辑表单的字段配置
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'transferId',
      dependencies: {
        triggerFields: ['transferId'],
        show: false,
      },
    },
    {
      component: 'Input',
      fieldName: 'transferCode',
      label: '转移单编号',
      rules: 'required',
      formItemClass: 'col-span-5',
      componentProps: {
        placeholder: '请输入转移单编号',
      },
      dependencies: {
        triggerFields: ['switch'],
        trigger(values) {
          values.transferCode = switchValue;
        },
      },
    },
    {
      component: 'Switch',
      fieldName: 'switch',
      label: '自动生成',
      formItemClass: 'col-span-2 col-start-6',
      labelWidth: 70,
      componentProps: {
        defaultValue: 0,
        onChange: (e: boolean) => {
          if (e) {
            getTransferCode().then((res) => {
              switchValue.value = res.data;
            });
          } else {
            switchValue.value = '';
          }
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'transferName',
      label: '转移单名称',
      rules: 'required',
      componentProps: {
        placeholder: '请输入转移单名称',
      },
      formItemClass: 'col-start-8 col-span-5',
    },

    {
      component: 'RadioGroup',
      componentProps: {
        options: mes_transfer_type,
      },
      fieldName: 'transferType',
      label: '转移单类型',
      defaultValue: 'INNER',
      formItemClass: 'col-span-5',
    },

    {
      component: 'DatePicker',
      fieldName: 'transferDate',
      label: '转移日期',
      formItemClass: 'col-span-4',
      rules: 'selectRequired',
    },

    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入备注信息',
        maxLength: 50,
        rows: 3,
        showCount: false,
      },
      fieldName: 'remark',
      label: '备注',
      emptyStateValue: null,
      formItemClass: 'col-span-12',
    },
  ];
}
/**
 * 获取表格列配置
 * @param onView (param: Transfer.Row) => void,
 * @param _onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onView: (param: Transfer.Row) => void,
  _onActionClick?: OnActionClickFn<Transfer.Row>,
): VxeTableGridOptions<Transfer.Row>['columns'] {
  return [
    {
      field: 'checkbox',
      fixed: 'left',
      type: 'checkbox',
      width: 50,
    },
    {
      field: 'transferCode',
      fixed: 'left',
      title: '转移单编号',
      cellRender: { name: 'CellButtonInfo', props: { onClick: onView } },
    },
    {
      field: 'transferName',
      fixed: 'left',
      title: '转移单名称',
    },
    {
      field: 'transferType',
      title: '转移单类型',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_transfer_type.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'deliveryFlag',
      title: '是否配送',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          sys_yes_no.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
      cellRender: { name: 'CellTag' },
    },
    {
      field: 'transferDate',
      title: '转移日期',
    },
    {
      field: 'status',
      title: '单据状态',
      formatter: ({ cellValue }) => {
        const statusMap = new Map(
          mes_transfer_status.map((option) => [option.value, option.label]),
        );
        return statusMap.get(cellValue) || '-';
      },
    },
    {
      field: 'operation',
      title: '操作',
      width: 200,
      fixed: 'right',
      align: 'center',
      slots: { default: 'action' },
    },
  ];
}
